<?php

/**
 * uniBoard ACP New Help File
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_new_help{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Available Parents
	 *
	 * @var array
	 */
	
	private $parents		= array();
		
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_help_file( $appCore);
			
		// Set file first parent
		$this -> parents[0] = $appCore -> lang['acp_help_file_parent_0'];
		
		// Select Files
		$appCore -> db -> query( 'SELECT file_id, file_name
		FROM ' . DB_PREFIX . 'help_files
		WHERE file_parent = \'0\'
		ORDER BY file_pos ASC');
		
		// Parse result
		while ( $file_result = $appCore -> db -> fetch_array())
		{
			$this -> parents[$file_result[0]] = $appCore -> ifString( htmlspecialchars( $file_result[1]));
		}
		
		// Free result
		$appCore -> db -> freeResult();
			
		// Update?
		if ( $appCore -> _POST( 'update') == 1)
		{
			// Fill Api
			$this -> api -> setFileName( $appCore -> _POST( 'file_name'));
			$this -> api -> setFileText( $appCore -> _POST( 'file_text'));
			$this -> api -> setFileParent( $appCore -> _POST( 'file_parent'));
			
			// Parent incorrect?
			if ( !isset( $this -> parents[$appCore -> _POST( 'file_parent')]))
			{
				$this -> api -> setError( $appCore -> lang['acp_help_file_parent_not_found']);
			}
			
			// Api All-rite?
			if ( $this -> api -> error)
			{
				// Draw Errors
				$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['new_help_file_form']);
				
				// Repeat Form
				$this -> drawForm();
			}
			else
			{
				// Save Api
				$this -> api -> saveData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_created_help_file', htmlspecialchars( $appCore -> _POST( 'file_name')));
				
				// Redirect us back to list
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=help_system'), $appCore -> lang['acp_help_file_added']);
			}
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_help'));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw Basic fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_help_file_parent'], $this-> appCore -> drawList( 'file_parent', $this -> parents, $this -> api -> file_parent));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_help_file_name'], $this-> appCore -> drawTextInput( 'file_name', $this -> api -> file_name));
		
		// Draw Text Field
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_help_file_text']);
		
		// Draw Row
		echo $this -> appCore -> output -> tableRow( $this -> appCore -> drawBigTextArea( 'file_text', $this -> api -> file_text));
			
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['new_help_file_form_submit']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['new_help_file_form'], array(
			$this -> appCore -> adminLink( 'act=help_system') => $this -> appCore -> lang['acp_help_system'],
			'' => $this -> appCore -> lang['edit_help_file_form']
		), ob_get_clean());
			
	}

}